
*Settings
clear all
cap log close
*Change this to your system name
local user "awcassidy1"
set more off, permanently
cd "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data"

use "C:\Users\awcassidy1\Dropbox\jmp_new\cleaned_data/data_with_observability_indices.dta", clear



********************************************************************************
*Baseline: observability groups
********************************************************************************
*standard values
local clustvar "mlsid"
local clustvarword "Home"
local fevars "i.saleyear i.lsaleyear i.yearbuiltcat"
local fe_to_absorb="saleyear lsaleyear yearbuiltcat"
local controls "yearbuiltaudit conditionedsqft"
local baselinevars "parsimonious_less parsimonious_more_fuel parsimonious_more_other"
local filename "main"
local depvar "pricediff"
local not_extracted="duct_type  rsavg  eeravg  sizeavg    negsysageavg ah_type notwinrec  notraddr"

local parsimonious_less_vars "negleakavg atticrvalue ductravg     "
local parsimonious_more_vars_fuel "fftype whftype "
local parsimonious_more_vars_other "ah_location progtherm twosystems"


********************************************************************************
*VAR DEFINITIONS AND LABELS
********************************************************************************


local non_less_vars duct_type  rsavg   sizeavg eeravg ///
	negsysageavg ah_type notwinrec  notraddr  fftype whftype  ah_location progtherm twosystems

gen dol_negleak=negleakavg*209
gen dol_atticrvalue=atticrvalue*71
gen dol_ductravg=ductravg*193
gen dol_eeravg=eeravg*959


gen dol_negleak_onepercent=negleakavg*409.36
gen dol_atticrvalue_onepercent=atticrvalue*166.02
gen dol_ductravg_onepercent=ductravg*377.95
gen dol_eeravg_onepercent=eeravg*1377.44


egen dol_excluding_eeravg=rowtotal(dol_negleak dol_atticrvalue dol_ductravg)
replace dol_excluding_eeravg = dol_excluding_eeravg/10000

la var dol_excluding_eeravg "Savings, 7% discount"

egen dol_excluding_eeravg_onepercent=rowtotal(dol_negleak_onepercent dol_atticrvalue_onepercent dol_ductravg_onepercent)
replace dol_excluding_eeravg_onepercent = dol_excluding_eeravg_onepercent/10000

la var dol_excluding_eeravg "Savings, 1% discount"


gen pre_dol_excluding_eeravg = pre*dol_excluding_eeravg
gen post_dol_excluding_eeravg = post*dol_excluding_eeravg

la var pre_dol_excluding_eeravg "Savings $\times$ Pre-sample"
la var post_dol_excluding_eeravg "Savings $\times$ Post"




gen dol_negleak_annual=negleakavg*15.70484651
gen dol_atticrvalue_annual=atticrvalue*5.006210884
gen dol_ductravg_annual=ductravg*14.5

egen dol_excluding_eeravg_annual=rowtotal(dol_negleak_annual dol_atticrvalue_annual dol_ductravg_annual)
replace dol_excluding_eeravg_annual = dol_excluding_eeravg_annual/10000

gen pre_annual_savings = pre*dol_excluding_eeravg_annual
gen post_annual_savings = post*dol_excluding_eeravg_annual

la var pre_annual_savings  "Annual Savings $\times$ Pre-sample"
la var post_annual_savings  "Annual Savings $\times$ Post"

la var dol_excluding_eeravg_annual  "Annual Savings"
rename dol_excluding_eeravg_annual annual_savings


		
*in an average year, a household uses 196 cubic feet.
*fuel_charge_ht is in $/mcf.

*fuel_charge_ht is verified by external source.
drop fc_ht_sale



********************************************************************************
*MUST DO FUEL SAVINGS PREP BEFORE THIS!!!
********************************************************************************

cap drop _merge
merge m:1 saleyear salemonth using "C:\Users/`user'\Dropbox\jmp_new\Austin Electricity and Gas Statistics\elec_average.dta", nogenerate keep(1 3)

merge m:1 saleyear salemonth using "C:\Users/`user'\Dropbox\jmp_new\Austin Electricity and Gas Statistics\nat_gas_average.dta", nogenerate keep(1 3)

*21.6 12.5 are the mmbtu for space heating and water heating respectively.
*according to EIA for south west south central (which texas is part of) https://www.eia.gov/consumption/residential/data/2015/c&e/pdf/ce3.1.pdf
gen gas_charge_for_year_space=gas_charge_per_mmbtu*21.6
gen elec_charge_for_year_space=elec_charge_per_mmbtu*21.6

gen gas_charge_for_year_water=gas_charge_per_mmbtu*12.5
gen elec_charge_for_year_water=elec_charge_per_mmbtu*12.5

gen annual_fuel_cost_fftype=gas_charge_for_year_space if fftype==1
replace annual_fuel_cost_fftype=elec_charge_for_year_space if fftype==0

gen annual_fuel_cost_whftype=gas_charge_for_year_water if whftype==1
replace annual_fuel_cost_whftype=elec_charge_for_year_water if whftype==0

gen annual_fuel_differential_fftype=(elec_charge_per_mmbtu-gas_charge_per_mmbtu) * 21.6
gen annual_fuel_differential_whftype=(elec_charge_per_mmbtu-gas_charge_per_mmbtu) * 12.5


sum annual_fuel_differential_fftype annual_fuel_differential_whftype

gen annual_fuel_savings=annual_fuel_differential_fftype*fftype+annual_fuel_differential_whftype*whftype

sum annual_fuel_savings


gen annual_fuel_cost = annual_fuel_cost_whftype+annual_fuel_cost_fftype

********************************************************************************
*If using above median ee all mean?
********************************************************************************
********************************************************************************
*Table with multiple columns that features above median ee all mean.
		
********************************************************************************
*Main plus placebo, with 3-sale, individual scores on their own.
********************************************************************************
********************************************************************************
*Less_st
********************************************************************************

eststo clear

*Main

eststo: areg pricediff annual_savings  i.yearbuiltcat i.saleyear  ///
	///
	, ///
		vce(cl mlsid) absorb( lsaleyear )	
	
estadd scalar r2all = e(r2)

estadd local sample="\multicolumn{1}{c}{Main}"
estadd local dep_var = "\multicolumn{1}{c}{$\Delta$ P}"
estadd local control_other_audit_measures =  "\multicolumn{1}{c}{No}"
estadd local control_yblt_sqft =  "\multicolumn{1}{c}{No}"

nlcom 1/(_b[annual_savings]-1)
local b=r(b)[1,1]
local v=r(V)[1,1]
local ci_lb=`b'-1.96*sqrt(`v')
local ci_ub=`b'+1.96*sqrt(`v')

local ci_lb: di %9.3fc `ci_lb'
local ci_ub: di %9.3fc `ci_ub'

estadd scalar implied_discount=`b'

estadd local implied_discount_ci=" [ `ci_lb', `ci_ub' ] "

eststo: areg pricediff annual_savings `non_less_vars'  i.yearbuiltcat i.saleyear  ///
	///
	, ///
		vce(cl mlsid) absorb( lsaleyear )	
	
estadd scalar r2all = e(r2)

estadd local sample="\multicolumn{1}{c}{Main}"
estadd local dep_var = "\multicolumn{1}{c}{$\Delta$ P}"
estadd local control_other_audit_measures =  "\multicolumn{1}{c}{Yes}"
estadd local control_yblt_sqft =  "\multicolumn{1}{c}{No}"

nlcom 1/(_b[annual_savings]-1)
local b=r(b)[1,1]
local v=r(V)[1,1]
local ci_lb=`b'-1.96*sqrt(`v')
local ci_ub=`b'+1.96*sqrt(`v')

local ci_lb: di %9.3fc `ci_lb'
local ci_ub: di %9.3fc `ci_ub'

estadd scalar implied_discount=`b'

estadd local implied_discount_ci=" [ `ci_lb', `ci_ub' ] "
		

local keepvars annual_savings

esttab using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/savings_main.tex", replace fragment ///
		keep(`keepvars') order(`keepvars') ///
		 label booktabs nomtitles noobs se(4) b(4) ///
		scalars( ///
			"implied_discount Discount Rate" ///
			"implied_discount_ci Discount Rate 95\% CI" ///
				"control_other_audit_measures Control for Other Audit Measures?" ///
				"r2all R-squared" ///
				"N Observations"  ///
				) ///
				sfmt(%9.3fc 0 0 %9.3fc  %9.0fc ) star(* 0.10 ** 0.05 *** 0.01)
